package com.csd.activity;

import com.csd.view.CarTypePopupwindow;
import com.csd.view.CsdPopupwindow;
import com.csd.view.MenuPopupwindow;
import com.csd.view.TimePopupwindow;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class EnterpriseApplayActivity extends Activity implements
		OnClickListener {

	private CsdApplication csdApp;
	private ActionBar actionBar;
	private LinearLayout ll_purpose, ll_taketime, ll_returntime, ll_cartype,
			ll_starting;
	private TextView tv_purpose, tv_taketime, tv_returntime, tv_cartype,
			tv_starting, purpose, taketime, returntime, cartype, starting;
	private String[] strPurpose = { "接送客户", "接送领导", "出差", "其它" };
	private String str[] = { "天山路641号慧谷白猫科技园", "中山西路139号停车场", "张杨路1307号" };
	private Button bt_commit;
	private ImageView img, msg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_enterpriseapply);
		init();
		initView();
	}

	private void init() {
		// TODO Auto-generated method stub
		if (csdApp == null) {
			csdApp = CsdApplication.getInstance();
		}
		csdApp.addActivity(this);
		ll_purpose = (LinearLayout) findViewById(R.id.ll_purpose);
		ll_taketime = (LinearLayout) findViewById(R.id.ll_taketime);
		ll_returntime = (LinearLayout) findViewById(R.id.ll_retruntime);
		ll_cartype = (LinearLayout) findViewById(R.id.ll_cartype);
		ll_starting = (LinearLayout) findViewById(R.id.ll_starting);
		ll_purpose.setOnClickListener(this);
		ll_taketime.setOnClickListener(this);
		ll_cartype.setOnClickListener(this);
		ll_starting.setOnClickListener(this);
		ll_returntime.setOnClickListener(this);
		tv_purpose = (TextView) findViewById(R.id.tv_purpose);
		tv_taketime = (TextView) findViewById(R.id.tv_taketime);
		tv_returntime = (TextView) findViewById(R.id.tv_retruntime);
		tv_cartype = (TextView) findViewById(R.id.tv_cartype);
		tv_starting = (TextView) findViewById(R.id.tv_starting);
		purpose = (TextView) findViewById(R.id.purpose);
		taketime = (TextView) findViewById(R.id.taketime);
		returntime = (TextView) findViewById(R.id.retruntime);
		cartype = (TextView) findViewById(R.id.cartype);
		starting = (TextView) findViewById(R.id.starting);
		bt_commit = (Button) findViewById(R.id.commit);
		bt_commit.setOnClickListener(this);
	}

	private void initView() {
		// TODO Auto-generated method stub
		actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(false);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayShowCustomEnabled(true);

		LayoutInflater inflator = (LayoutInflater) getSystemService(this.LAYOUT_INFLATER_SERVICE);

		View v = inflator.inflate(R.layout.actionbar_main, null);
		TextView title = (TextView) v.findViewById(R.id.title);
		title.setText("企业用车申请");
		msg = (ImageView) v.findViewById(R.id.msg);
		msg.setImageDrawable(getResources().getDrawable(R.drawable.u88));
		msg.setOnClickListener(this);
		img = (ImageView) v.findViewById(R.id.logo);
		img.setImageDrawable(getResources().getDrawable(R.drawable.u86));
		img.setOnClickListener(this);
		ActionBar.LayoutParams layout = new ActionBar.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

		actionBar.setCustomView(v, layout);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.ll_purpose:
			CsdPopupwindow purposePop = new CsdPopupwindow(this, tv_purpose,
					strPurpose, purpose, 300);
			purposePop.showList();
			purposePop.showPopupWindow(ll_purpose);
			break;
		case R.id.ll_taketime:
			TimePopupwindow takeTime = new TimePopupwindow(this, tv_taketime);
			takeTime.showPopupWindow(ll_taketime);
			break;
		case R.id.ll_retruntime:
			TimePopupwindow returntime = new TimePopupwindow(this,
					tv_returntime);
			returntime.showPopupWindow(ll_returntime);
			break;
		case R.id.ll_cartype:
			CarTypePopupwindow cartypePop = new CarTypePopupwindow(this,
					tv_cartype, cartype);
			cartypePop.showPopupWindow(ll_cartype);
			break;
		case R.id.ll_starting:
			CsdPopupwindow csdPop = new CsdPopupwindow(this, tv_starting, str,
					starting, 600);
			csdPop.showList();
			csdPop.showPopupWindow(ll_starting);
			break;
		case R.id.commit:
			Intent intent = new Intent(this, EnterpriseOrderActivity.class);
			intent.putExtra("num", 1);
			startActivity(intent);
			break;
		case R.id.logo:
			finish();
			break;
		case R.id.msg:
			MenuPopupwindow menu = new MenuPopupwindow(this);
			menu.showPopupWindow(msg);

			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(EnterpriseApplayActivity.this,
					MainActivity.class);
			intent.putExtra("num", 0);
			intent.putExtra("flag", 0);
			startActivity(intent);
			csdApp.exit();
		}
		return super.onKeyDown(keyCode, event);
	}

}
